package com.auto.qinglong.net.client.beans

import com.auto.qinglong.bean.panel.PanelTask
import com.auto.qinglong.net.panel.v15.TasksRes
import com.auto.qinglong.utils.CronUnit
import com.auto.qinglong.utils.TimeUnit
import java.util.Locale


fun TasksRes.TaskObject.toPanel(): PanelTask {
    val task = PanelTask()
    task.key = id
    task.name = name
    task.isPinned = isPinned == 1
    task.command = command
    task.schedule = schedule
    //任务上次执行时间
    if (lastExecutionTime > 0) {
        task.lastExecuteTime = TimeUnit.formatDatetimeA(lastExecutionTime * 1000)
    } else {
        task.lastExecuteTime = "--"
    }

    //任务上次执行时长
    if (last_running_time >= 60) {
        task.lastRunningTime = String.format(
            Locale.CHINA,
            "%d分%d秒",
            last_running_time / 60,
            last_running_time % 60
        )
    } else if (last_running_time > 0) {
        task.lastRunningTime = String.format(Locale.CHINA, "%d秒", last_running_time)
    } else {
        task.lastRunningTime = "--"
    }

    //任务下次执行时间
    task.nextExecuteTime = CronUnit.nextExecutionTime(schedule, "--")

    // 任务状态
    if (status == 0f) {
        task.state = "运行中"
        task.stateCode = PanelTask.STATE_RUNNING
    } else if (isDisabled == 1) {
        task.state = "已禁止"
        task.stateCode = PanelTask.STATE_LIMIT
    } else if (status == 0.5f || status == 3f) {
        task.state = "队列中"
        task.stateCode = PanelTask.STATE_WAITING
    } else {
        task.state = "空闲中"
        task.stateCode = PanelTask.STATE_FREE
    }
    return task
}